
package com.lyon.custom.core;

import com.lyon.custom.common.constants.PropertyChangeType;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
/**
 * @author LeeYan9
 * @since 2021-11-26
 */
@SuppressWarnings("all")
public abstract class AbstractConfigChangeParser implements ConfigChangeParser {
    private final String configType;

    public AbstractConfigChangeParser(String configType) {
        this.configType = configType;
    }

    @Override
    public boolean isResponsibleFor(String type) {
        return this.configType.equalsIgnoreCase(type);
    }

    protected Map<String, ConfigChangeItem> filterChangeData(Map oldMap, Map newMap) {
        Map<String, ConfigChangeItem> result = new HashMap(16);
        Iterator var4 = oldMap.entrySet().iterator();

        while(true) {
            Entry e;
            ConfigChangeItem cci;
            while(true) {
                if (!var4.hasNext()) {
                    var4 = newMap.entrySet().iterator();

                    while(var4.hasNext()) {
                        e = (Entry)var4.next();
                        if (!oldMap.containsKey(e.getKey())) {
                            cci = new ConfigChangeItem((String)e.getKey(), (String)null, e.getValue().toString());
                            cci.setType(PropertyChangeType.ADDED);
                            result.put((String) e.getKey(), cci);
                        }
                    }

                    return result;
                }

                e = (Entry)var4.next();
                if (newMap.containsKey(e.getKey())) {
                    if (e.getValue().equals(newMap.get(e.getKey()))) {
                        continue;
                    }

                    cci = new ConfigChangeItem((String)e.getKey(), e.getValue().toString(), newMap.get(e.getKey()).toString());
                    cci.setType(PropertyChangeType.MODIFIED);
                    break;
                }

                cci = new ConfigChangeItem((String)e.getKey(), e.getValue().toString(), (String)null);
                cci.setType(PropertyChangeType.DELETED);
                break;
            }

            result.put((String) e.getKey(), cci);
        }
    }
}
